package com.atguigu.yygh.order.controller;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.utils.JwtHelper;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.service.OrderInfoService;
import com.atguigu.yygh.vo.order.OrderCountQueryVo;
import com.atguigu.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-10-07
 */
@RestController
@RequestMapping("/user/order")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    @GetMapping("/{scheduleId}/{patientId}")
    public R submitOrder(@PathVariable String scheduleId, @PathVariable Long patientId){
        Long orderId = orderInfoService.submitOrder(scheduleId,patientId);
        return R.ok().data("orderId",orderId);
    }

    @GetMapping("/getOrders/{orderId}")
    public R getOrders(@PathVariable Long orderId){
        OrderInfo orderInfo = orderInfoService.getById(orderId);
        orderInfo.getParam().put("orderStatusString",OrderStatusEnum.getStatusNameByStatus(orderInfo.getOrderStatus()));
        return R.ok().data("orderInfo",orderInfo);
    }

    @GetMapping("/pageList/{pageNum}/{pageSize}")
    public R pageList(OrderQueryVo queryVo , @RequestHeader String token ,
                      @PathVariable Integer pageNum,@PathVariable Integer pageSize){
        Long userId = JwtHelper.getUserId(token);
        queryVo.setUserId(userId);
        Page<OrderInfo> page = orderInfoService.selectPage(pageNum,pageSize,queryVo);
        return R.ok().data("page",page);
    }

    @GetMapping("/getStatusList")
    public R getStatusList(){
        List<Map<String, Object>> list = OrderStatusEnum.getStatusList();
        return R.ok().data("list",list);
    }

    @PutMapping("/cancelOrder/{orderId}")
    public R cancelOrder(@PathVariable Long orderId) throws Exception {
        orderInfoService.cancelOrder(orderId);
        return R.ok();
    }

    @PostMapping("/getOrderCountList")
    public Map<String,Object> getOrderCountList(@RequestBody OrderCountQueryVo orderCountQueryVo){
        return orderInfoService.getOrderCountList(orderCountQueryVo);
    }
}

